package controller.meet;

import controller.BaseServlet;
import model.Meeting;
import model.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;
import pojo.PageEntity;
import service.MeetingService;
import service.UserService;
import service.impl.MeetingServiceImpl;
import service.impl.UserServiceImpl;
import utils.DateStrConverter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;


@WebServlet("/meeting/*")
public class MeetingServlet extends BaseServlet {

    private MeetingService meetingService = new MeetingServiceImpl();
    private UserService userService = new UserServiceImpl();

    public void findPage(HttpServletRequest req, HttpServletResponse resp){
        try{
            Map<String, String> params = getParams(req);
            Integer currPage;
            //当前页
            if(params.get("currPage") == null || params.get("currPage").equals("")) {
                currPage = 1;
            }else{
                currPage = Integer.parseInt(params.get("currPage"));
            }
            //标题
            String title = params.get("title");
            if(title == null){
                title = "";
            }
            //会议状态
            Integer status = null;
            if (params.get("status") != null && !"".equals(params.get("status"))) {
                status = Integer.parseInt(params.get("status"));
            }
            // 会议分页信息
            PageEntity<Meeting> pageData = meetingService.findPage(currPage, title, status);
            req.setAttribute("pageData", pageData);

            // 搜索条件回显
            req.setAttribute("title", title);
            req.setAttribute("status", status);
            req.getRequestDispatcher("/view/meeting").forward(req, resp);

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void add(HttpServletRequest req, HttpServletResponse resp){
        Map<String, String> params = getParams(req);
        try {
            Meeting meeting = new Meeting();
            //类型转换工具类
            ConvertUtilsBean convertUtilsBean = new ConvertUtilsBean();
            convertUtilsBean.register(new DateStrConverter(), Date.class);
            BeanUtilsBean beanUtils = new BeanUtilsBean(convertUtilsBean);
            beanUtils.populate(meeting, params);
            meetingService.save(meeting);
            resp.sendRedirect("/meeting/findPage?currPage=1");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void meetingDetail(HttpServletRequest req, HttpServletResponse resp){
        try{
            Map<String, String> params = getParams(req);
            Integer id = Integer.valueOf(params.get("id"));
            Meeting meeting = meetingService.findById(id);
            String[] joinUser = meeting.getMakeUser().split(",");
            //实到人数
            Integer realCount = meetingService.findMeetingCountByMeetingId(id);
            //未到人数
            Integer noCount = joinUser.length - realCount;
            //是否参加
            User user = (User)req.getSession().getAttribute("loginUser");
            Boolean flag = meetingService.checkJoin(user.getId(), id);
            req.setAttribute("meeting",meeting);
            req.setAttribute("joinCount",joinUser.length);
            req.setAttribute("realCount",realCount);
            req.setAttribute("noCount",noCount);
            req.setAttribute("flag",flag);

            req.getRequestDispatcher("/view/meetingDetail").forward(req,resp);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void joinMeeting(HttpServletRequest req, HttpServletResponse resp){
        try{
            Map<String, String> params = getParams(req);
            Integer id = Integer.valueOf(params.get("id"));
            User user = (User)req.getSession().getAttribute("loginUser");
            Boolean isJoinMeeting = meetingService.checkJoin(user.getId(), id);
            if(isJoinMeeting){
                meetingService.unJoinMeeting(user.getId(), id);
            }else{
                meetingService.joinMeeting(user.getId(), id);
            }
            resp.sendRedirect("/meeting/meetingDetail?id="+id);
            req.setAttribute("id", id);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
